﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;

namespace WindowsFormsApplication1
{
    public partial class Stock : Form
    {
        public Stock()
        {
            InitializeComponent();
        }

        private void Stock_Load(object sender, EventArgs e)
        {
            DbConnet.connect(new Log().getConstr());
        }

        private void textBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            new Item_Popup().ShowDialog();
            txtitem.Text = new Midval().getItemName();
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Boolean upd = false;
            Boolean ins = false;
            Double itmsih = 0;
            try
            {
                String val = txtitem.Text.Trim();
                val = val.Substring(0, val.IndexOf(" - "));

                String getItms = "SELECT ISNULL(noofitems,0) noi FROM stock WHERE Itemcode = '" + val + "'";
                SqlDataReader idr = DbConnet.ReadTable(getItms);
                while (idr.Read())
                {
                    itmsih = Double.Parse(idr["noi"].ToString());
                }
                idr.Close();

                Double newCo = 0;
                try
                {
                    newCo = Double.Parse(txtNoOfItems.Text.Trim());
                }
                catch
                {
                    newCo = 0;
                }


                Double tot = newCo + itmsih;

                String updStk = "UPDATE stock SET noofitems ='"+tot+"',lastupdateted = '"+DateTime.Now+"' WHERE Itemcode = '"+val+"'";
                String insStk = "INSERT INTO stock (Itemcode,noofitems,lastupdateted) " +
                                " VALUES ('" + val + "','" + tot + "','" + DateTime.Now + "')";

                upd = DbConnet.AddEditDel(updStk);

                if (!upd)
                    ins = DbConnet.AddEditDel(insStk);

                if (upd || ins)
                {
                    MessageBox.Show(new Form() { TopMost = true }, "Stock Details Added Update Successfully ", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtitem.Clear();
                    txtNoOfItems.Clear();
                }
            }
            catch (Exception exstck)
            {
            }
        }

        private void txtitem_TextChanged(object sender, EventArgs e)
        {
            try
            {
                String itm = txtitem.Text.Trim();
                itm = itm.Substring(0, itm.IndexOf(" - "));
                String getStk = "SELECT Itemcode,noofitems FROM stock WHERE Itemcode = '"+itm+"'";
                SqlDataReader std = DbConnet.ReadTable(getStk);
                if (std.Read())
                {
                    txtNoOfItems.Text = std["noofitems"].ToString();
                }
                std.Close();
            }
            catch (Exception exSt)
            {
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            View_Stock vs = new View_Stock();
            vs.ShowDialog();
        }
    }
}
